import cv2
import numpy as np

img1 = cv2.imread("d:\\test\\messi5.jpg")
img2 = cv2.imread('d:\\test\\opencv-logo-white.png')

#量出目标图B的尺寸
rows, cols, channels = img2.shape
#获取图A的相应区域
roi = img1[0:rows, 0:cols]

#将目标图B灰度
img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
#通过设置阈值，将不白的全白，不太黑的全黑
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)

# print(mask)
#取反，黑的变白，白的变黑
mask_inv = cv2.bitwise_not(mask)

#把兴趣图，全黑的，附在图A相应区域上
img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
#把图B中兴趣点区域还原成彩色，其他区域全黑（无色？）
img2_fg = cv2.bitwise_and(img2, img2, mask=mask)

#将图A相应区域融合
dst = cv2.add(img1_bg, img2_fg)

#将图A中指定区域替换
img1[0:rows, 0:cols] = dst

cv2.imshow('res', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()